package org.glut.saltedfish.domain;

import com.google.gson.GsonBuilder;
import lombok.Data;

/**
 * <p>
 * 统一返回
 * </p>
 *
 * @author mjl
 * @since 2025/7/17
 */
@Data
public class ApiResponse<T> {
    private int code;       // 状态码
    private String message; // 消息
    private long timestamp; // 时间戳
    private T data;         // 数据

    public ApiResponse() {
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> ApiResponse<T> success(T data) {
        ApiResponse<T> response = new ApiResponse<>();
        response.setCode(200);
        response.setMessage("success");
        response.setData(data);
        return response;
    }

    public static <T> ApiResponse<T> success(String message, T data) {
        ApiResponse<T> response = new ApiResponse<>();
        response.setCode(200);
        response.setMessage(message);
        response.setData(data);
        return response;
    }

    public static ApiResponse<Void> error(int code, String message) {
        ApiResponse<Void> response = new ApiResponse<>();
        response.setCode(code);
        response.setMessage(message);
        return response;
    }

    public static ApiResponse<Void> error(String message) {
        return error(500, message);
    }
    public String toJson() {
        return new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .create()
                .toJson(this);
    }
}
